/*
 *所有关于tb_project_expectedresults类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*tb_project_expectedresultsService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using MYData.Entity.DomainModels;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using MYData.System.IRepositories;
using System.Collections.Generic;
using Mapster;
using System;
using MYData.Entity.DTO.ApiEntity.Input;
using MYData.Core.EFDbContext;
using System.Linq;

namespace MYData.System.Services
{
    public partial class tb_project_expectedresultsService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Itb_project_expectedresultsRepository _repository;//访问数据库
        private readonly VOLContext _db;
        [ActivatorUtilitiesConstructor]
        public tb_project_expectedresultsService(
            Itb_project_expectedresultsRepository dbRepository,
            IHttpContextAccessor httpContextAccessor,
            VOLContext db
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            _db = db;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        /// <summary>
        /// 项目预期成果表多添加
        /// </summary>
        /// <param name="list"></param>
        public void AddList(IEnumerable<tb_project_expectedresults_Dto> list,string projectId)
        {
            var model = list.Adapt<IEnumerable<tb_project_expectedresults>>();
            foreach (var item in model)
            {
                //var key= new Snowflake.Core.IdWorker(1, 1).NextId().ToString();
                var key = Guid.NewGuid().ToString();
                item.id = key;
                item.projectId = projectId;
                _repository.Add(item, false);
            }
            //_repository.AddRange(model, false);
        }
        /// <summary>
        /// 项目预期成果表多修改
        /// </summary>
        /// <param name="list"></param>
        public void UptList(IEnumerable<tb_project_expectedresultsUpdate_Dto> list,string projectId)
        {
     
            var model = list.Adapt<IEnumerable<tb_project_expectedresults>>();
            var DelList1 = _db.Set<tb_project_expectedresults>().Where(x => x.projectId == projectId).ToList();
            //单个删除(清空表数据)
            foreach (var item1 in DelList1)
            {
                _repository.Delete(item1, false);
            }
            foreach (var item in model)
            {
                //var key= new Snowflake.Core.IdWorker(1, 1).NextId().ToString();
                var key = Guid.NewGuid().ToString();
                item.id = key;
                item.projectId = projectId;
                _repository.Add(item, false);
            }
            //_repository.UpdateRange(model, false);
        }


    }
}
